const express = require('express');
const router = express.Router();
// const multer = require("multer");
// const key = require('../../config/keys')
const db = require('../../config/db.js')
const config = require('../../config/require')
const HostUrl = `${config.PROTOCOL}://${config.HOST}:${config.PORT}`


//上传图片的包
let multer = require('multer')

// 通过filename属性定制
let storage = multer.diskStorage({
    // 保存的路径
    destination: function (req, file, callback) {
        callback(null, 'public/uploads/')
    },
    filename: function (req, file, callback) {
        // 将保存文件名设置为 字段名 + 时间戳，比如 logo-1478521468943
        let suffix = file.mimetype.split('/')[1]; //获取文件格式
        callback(null, file.fieldname + '-' + Date.now() + '.' + suffix);
    }
});

//生成的图片放入uploads文件夹下
let upload = multer({
    storage: storage
})

router.get('/taskList', function (req, res) {
    let check_sql = 'select * from task';
    db.query(check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                message: '查询失败'
            })
        } else {
            res.send({
                code: 200,
                message: '查询成功',
                rows
            })
        }
    })
})

router.get('/myTaskList', function (req, res) {
    let taskId = req.query.taskId
    let check_sql = 'select * from task where task_id = "' + taskId + '"';
    db.query(check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                message: '查询失败'
            })
        } else {
            res.send({
                code: 200,
                message: '查询成功',
                rows
            })
        }
    })
})

// 根据id查询
router.get('/detail', function (req, res) {
    let taskId = req.query.taskId
    let check_sql = 'select * from task where task_id="' + taskId + '"';

    db.query(check_sql, (err, rows) => {
        if (err) {
            res.send({
                code: -1,
                message: '查询失败'
            })
        } else {
            res.send({
                code: 200,
                message: '查询成功',
                rows
            })
        }
    })
})




// 添加任务
router.post('/addTask', upload.single('photo'), function (req, res) {
    let classify = req.body.classify
    let introduce = req.body.introduce
    let synopsis = req.body.synopsis
    let title = req.body.title
    let price = req.body.price
    let num = req.body.num
    let total = req.body.total
    let link = req.body.link
    let step = req.body.step
    let start_data = req.body.start_data
    let end_data = req.body.end_data
    let task_id = Math.random().toString().slice(2)
    let uid = req.body.uid
    // 接收前端的文件
    let file = req.file
    // 定义存在在服务器的图片的路径（存在数据库的路径）
    let url = `${HostUrl}/static/uploads/${file.filename}`

    let insert_sql = "insert into task(classify,introduce,synopsis,title,price,num,total,link,step,start_data,end_data,task_id,url,uid) values('" + classify + "','" + introduce + "','" + synopsis + "','" + title + "','" + price + "','" + num + "','" + total + "','" + link + "','" + step + "','" + start_data + "','" + end_data + "','" + task_id + "','" + url + "','" + uid + "')"
    db.query(insert_sql,function(err,rows){
        if (err) {
            console.log(err)
        } else {
            res.send({
                code: 200,
                msg: '发布任务成功',
                
            })
        }
    })
})

// 更新任务数量
router.post('/updata_num',function(req,res) {
    let num = req.body.num 
    let taskId = req.body.taskId
    let check_sql = 'select * from task where task_id="' + taskId + '"';
    db.query(check_sql,(err,rows) =>{
        if(err) {
            res.send({
                code: -1,
                msg:'查询失败'
            })
        }else {
            if(num>0) {
                let updata_num_sql = 'update task set num="'+num+'" where task_id="'+taskId+'"'
                db.query(updata_num_sql,(err,rows) =>{
                    if(err) {
                        res.send({
                            code: 201,
                            msg:'更新失败'
                        })
                    }else {
                        res.send({
                            code:200,
                            msg:'更新成功'
                        })
                    }
                })
            }
            // else {
            //     let delect_sql = 'delete  from task where task_id = "'+taskId+'"'
            //     db.query(delect_sql,(err,rows) =>{
            //         if(err) {
            //             res.send({
            //                 code: 202,
            //                 msg:'删除失败'
            //             })
            //         }else {
            //             res.send({
            //                 code: 200,
            //                 msg:'删除成功'
            //             })
            //         }
            //     })
            // }
        }
    })
})

// 查询任务
router.post('/check_task',function(req,res) {
    let value = req.body.value
    let sql = 'SELECT * from task where title like "%'+value+'%"'
    db.query(sql,(err,rows) =>{
        if(err) {
            res.send({
                code:-1,
                msg:'查询失败'
            })
        }else {
            res.send({
                code:200,
                msg:'查询成功',
                rows
            })
        }
    })
})
module.exports = router